#!/bin/bash
. "$(dirname $0)/utils.sh"
run-safe
assure-extra-opkg-repo
if ! which svn > /dev/null ; then opkg install subversion ; fi

LOG "Get ccl from it's svn"
mkdir -p /usr/local/src
cd /usr/local/src
svn co http://svn.clozure.com/publicsvn/openmcl/trunk/linuxarm/ccl
cd ccl

LOG " ... Discard the svn info, doc, contrib, objc, and some other platforms' stuff"
rm -rf $( find . -name .svn )
rm -rf doc level-0/{PPC,X86} contrib objc-bridge

LOG "Put the executable on the path, /usr/local/bin"
LOG "   as both armcl, and lisp"
rm -f /usr/local/bin/armcl /usr/local/bin/lisp
ln -s $(pwd)/scripts/ccl /usr/local/bin/armcl
ln -s /usr/local/bin/armcl /usr/local/bin/lisp

LOG "Rebuild it from scratch."
LOG " ... via make"
cd /usr/local/src/ccl/lisp-kernel/linuxarm/
make clean
make
LOG "  ... via ccl:rebuild-ccl"
armcl <<EOF
(ccl:rebuild-ccl :full t)
(quit)
EOF

LOG "Setup quicklisp"
wget http://beta.quicklisp.org/quicklisp.lisp > /tmp/quicklisp.lisp
armcl <<EOF
(load "/tmp/quicklisp.lisp")
(funcall (read "quicklisp-quickstart:install"))
(quit)
EOF
rm /tmp/quicklisp.lisp
cat <<EOF > ~/.ccl-init.lisp
(let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp"
                                         (user-homedir-pathname))))
    (when (probe-file quicklisp-init)
      (load quicklisp-init)))
EOF
lisp <<EOF
(ql:quickload "quicklisp-slime-helper")
(quit)
EOF
mkdir -p ~/.emacs.d
cat <<EOF >> ~/.emacs.d/init.el
(load (expand-file-name "~/quicklisp/slime-helper.el"))
EOF
